﻿<template>
    <el-col :sm="24" :md="pzoption.mdwidth">
        <i class="iconfont icon-shezhi pull-right widgetset hidden-print" @click.stop="dialogInputVisible = true"></i>
        <i class="iconfont icon-shanchu pull-right widgetdel hidden-print" @click.stop="delWid(pzoption.wigdetcode)"></i>
        <el-form-item :label="pzoption.title"  :prop="'wigetitems.' + index + '.value'" :rules="childpro.rules">
            <el-input v-model="pzoption.value" style="display:none">
            </el-input>
            <el-select placeholder="请选择系部" style="width:100%" @change="selchange" v-model="pzoption.value">
                <el-option v-for="item in xbdata" :key="item.xbdm" :label="item.xbmc" :value="item.xbdm">
                </el-option>
            </el-select>
            <!--<el-select placeholder="请选择系部" ref="MselectXB" style="width:100%" v-if="childpro.dx" size="small" @change="selchange" v-model="childpro.mvalue" multiple collapse-tags>
                <el-option v-for="item in xbdata" :key="item.xbdm" :label="item.xbmc" :value="item.xbdm">
                </el-option>
            </el-select>-->
        </el-form-item>
        <el-dialog title="组件属性" :visible.sync="dialogInputVisible" width="40%">

            <!--<el-form-item label="是否多选">
                <el-switch v-model="childpro.dx" size="mini" style="width:100%">
                </el-switch>
            </el-form-item>-->
            <el-form-item label="是否教学系部">
                <el-switch v-model="childpro.isjx" size="mini" style="width:100%">
                </el-switch>
            </el-form-item>
            <el-form-item label="是否课程系部">
                <el-switch v-model="childpro.iskc" size="mini" style="width:100%">
                </el-switch>
            </el-form-item>
            <el-form-item label="是否权限系部">
                <el-switch v-model="childpro.isqx" size="mini" style="width:100%">
                </el-switch>
            </el-form-item>
            <el-form-item label="下拉框更改时执行代码">
                <el-input v-model="childpro.jscode" :autosize="{ minRows: 6, maxRows: 9}" autocomplete="off" type="textarea"></el-input>
            </el-form-item>

        </el-dialog>
    </el-col>

</template>
<script>
    module.exports = {
        props: ['pzoption', 'index'],
        data: function () {
            return {
                dialogInputVisible: false,
                xbdata: [],
                childpro: {
                    name:"系部组件",
                    mvalue: [],
                    placeholder: "请选择",
                    itemtype: "text",
                    dx: false,
                    isqx: true,
                    isjx: false,
                    iskc: false,
                    jscode: "",
                }
            }
        },
        methods: {
            delWid: function (wigdetcode) {
                // 子组件中触发父组件方法ee并传值cc12345
                this.$root.nowwidget = {};
                _.remove(this.$root.FormData.wigetitems, function (obj) {
                    return obj.wigdetcode == wigdetcode;
                });
            },
            selchange: function () {
                try {
                    var data = JSON.parse(JSON.stringify(this.pzoption));
                    let jscode = this.childpro.jscode;
                    let func = new Function('data', jscode);
                    func(data)
                } catch (e) {
                    app.$notify({
                        title: '成功',
                        message: '解析JS代码有误',
                        type: 'success'
                    });
                }

            },
            initwig: function () {
                var chi = this;
                var tempxbdata = JSON.parse(localStorage.getItem("xbdata"));
                var xbdata = [];
                if (chi.pzoption.childpro.isqx) {
                    //先过滤掉没有权限的
                    _.forEach(tempxbdata, function (obj) {
                        if (obj.qxxb == '1') {
                            xbdata.push(obj);
                        }
                    })
                } else {
                    xbdata = tempxbdata;
                }
                var temp = [];
                if (chi.pzoption.childpro.isjx) {
                    _.forEach(xbdata, function (obj) {
                        if (obj.jxxb) {
                            temp.push(obj);
                        }
                    })
                }
                else {
                    temp = xbdata;
                }
                var templast = [];
                if (chi.pzoption.childpro.iskc) {
                    _.forEach(temp, function (obj) {
                        if (obj.kcxb == '1') {
                            templast.push(obj);
                        }
                    })
                }
                else {
                    templast = temp;
                }
                chi.xbdata = templast;
                //新增和设计页面处理默认值
                if (chi.$root.pagemodel == "0" || chi.$root.pagemodel == "1") {
                    if (chi.xbdata.length > 0) {
                        if (chi.pzoption.childpro.dx) {
                            chi.pzoption.childpro.mvalue = [];
                            _.forEach(chi.xbdata, function (obj) {
                                chi.pzoption.childpro.mvalue.push(obj.xbdm);
                            })
                        } else {
                            chi.pzoption.value = chi.xbdata[0].xbdm;
                            chi.pzoption.valuetext = chi.xbdata[0].xbmc;

                        }
                    }
                }
                

            }
        },
        mounted: function () {
            var chi = this;
            chi.$nextTick(function () {
             
                if (chi.pzoption.childpro.name) {
                    //检测新增属性
                    _.forIn(chi.childpro, function (value, key) {
                        if (!_.has(chi.pzoption.childpro, key)) {
                            chi.pzoption.childpro[key] = value;
                        }

                    });
                    chi.childpro = chi.pzoption.childpro;
                } else {
                    chi.pzoption.childpro = chi.childpro;
                }
                //初始化方法
                chi.initwig();
                //初始化方法

            })

        },
        watch: {
            'childpro.mvalue': { //深度监听，可监听到对象、数组的变化
                handler(newV, oldV) {
                    if (newV && this.pzoption.childpro.dx) {
                        this.pzoption.value = newV.join();

                        $(this.$el).find("input").val("");//解决显示bug
                    }
                },
                deep: true
            },
            'pzoption.value': { //深度监听，可监听到对象、数组的变化
                handler(newV, oldV) {
                    if (newV) {
                        var chi = this;
                        var tempxbdata = JSON.parse(localStorage.getItem("xbdata"));

                        chi.pzoption.valuetext = _.find(tempxbdata, function (obj) {
                            return obj.xbdm == newV;
                        }).xbmc;
                    }
                },
                deep: true
            },
        }
    };
</script>